//
//  SettingsLayer.m
//  iLinkPair
//
//  Created by Yangss on 2010-06-10.
//  Copyright 2010 LotusVille. All rights reserved.
//

#import "SettingsLayer.h"
#import "mainpage.h"

static BOOL g_bMusic = YES;	//音乐开关全局变量
static BOOL g_bSound = YES;	//音效开关全局变量

@implementation SettingsLayer
-(id) init
{
	self = [super init];
	if (!self) {
		return nil;
	}
	
	//音效选项
	[CCMenuItemFont setFontName: @"American Typewriter"];
	[CCMenuItemFont setFontSize:28];
	CCMenuItemFont *titleSound = [CCMenuItemFont itemFromString: @"Sound"];
    [titleSound setIsEnabled:NO];
	[CCMenuItemFont setFontName: @"Marker Felt"];
	[CCMenuItemFont setFontSize:36];
    CCMenuItemToggle *itemSound = [CCMenuItemToggle itemWithTarget:self selector:@selector(menuSetSoundCallback:) items:
                             [CCMenuItemFont itemFromString: @"ON"],
                             [CCMenuItemFont itemFromString: @"OFF"],
                             nil];
	itemSound.selectedIndex = g_bSound==YES?0:1;		//使用当前音效状态选中菜单
    
	//声音选项
	[CCMenuItemFont setFontName: @"American Typewriter"];
	[CCMenuItemFont setFontSize:28];
	CCMenuItemFont *titleMusic = [CCMenuItemFont itemFromString: @"Music"];
    [titleMusic setIsEnabled:NO];
	[CCMenuItemFont setFontName: @"Marker Felt"];
	[CCMenuItemFont setFontSize:36];
    CCMenuItemToggle *itemMusic = [CCMenuItemToggle itemWithTarget:self selector:@selector(menuSetMusicCallback:) items:
                             [CCMenuItemFont itemFromString: @"ON"],
                             [CCMenuItemFont itemFromString: @"OFF"],
                             nil];
	itemMusic.selectedIndex = g_bMusic==YES?0:1;		//使用当前背景音乐状态选中菜单
    
    //空行，美化界面
	[CCMenuItemFont setFontSize:36];
	CCMenuItemFont *titleNil = [CCMenuItemFont itemFromString: @" "];
	[titleNil setIsEnabled:NO];
	
	//退回
	[CCMenuItemFont setFontName: @"Marker Felt"];
	[CCMenuItemFont setFontSize:28];
	CCMenuItemFont *buttonBack = [CCMenuItemFont itemFromString: @"Go back" target:self selector:@selector(backCallback:)];
    [buttonBack setIsEnabled:YES];
    
	CCMenu *menu = [CCMenu menuWithItems: 
				titleSound, titleMusic,
				itemSound, itemMusic,
				titleNil,
				buttonBack, nil]; // 6 items.
    [menu alignItemsInColumns:
     [NSNumber numberWithUnsignedInt:2],
     [NSNumber numberWithUnsignedInt:2],
     [NSNumber numberWithUnsignedInt:1],
     [NSNumber numberWithUnsignedInt:1],
     nil
	 ]; // 2 + 2 + 1 + 1 = total count of 6.
    
	[self addChild: menu];
	
	return self;
}

- (void) dealloc
{
	[super dealloc];
}

/*
-(void) menuCallback: (id) sender
{
	NSLog(@"selected item: %@ index:%d", [sender selectedItem], [sender selectedIndex] );
}
*/

//背景音乐设置回调函数
-(void) menuSetMusicCallback: (id) sender
{
	g_bMusic = [sender selectedIndex]==0?YES:NO;
	NSLog(@"Music:%d", g_bMusic);
}

//音效设置回调函数
-(void) menuSetSoundCallback: (id) sender
{
	g_bSound = [sender selectedIndex]==0?YES:NO;
	NSLog(@"Sound:%d", g_bSound);
}

-(void) backCallback: (id) sender
{
	CCScene *sc = [CCScene node];
	[sc addChild:[mainpage node]];
	
	[[CCDirector sharedDirector] replaceScene:  [CCZoomFlipYTransition transitionWithDuration:0.5f scene:sc]];
//	[[CCDirector sharedDirector] popScene];
}
@end
